package com.xhui.vimarket.zkplus.utils.zul;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;

import com.xhui.vimarket.common.utils.obj.BeanUtilsExtend;

/**
 * ComboboxUtil.
 * 
 * @author wuyx
 * @version Revision 1.0.0
 * 
 */
public final class ComboboxUtil {
    
    /**
     * LABEL_ALL.
     */
    public static final String LABEL_ALL = "全部";
    
    /**
     * 构造函数.
     * 
     */
    private ComboboxUtil() {
    }
    
    /**
     * setOrCreateValue.
     * 
     * @param cbox Combobox
     * @param val Object
     * @param label String
     */
    public static void setOrCreateValue(final Combobox cbox, final Object val, final String label) {
        if (cbox != null && val != null) {
            final List<Component> clist = cbox.getChildren();
            boolean flag = true;
            for (final Component citem : clist) {
                if (cbox.getValue() == null) { // 空值
                    continue;
                }
                if (val.equals(cbox.getValue())) { // 找到，选中
                    cbox.setSelectedItem((Comboitem)citem);
                    flag = false;
                    break;
                }
            }
            if (flag) { // 自动增加并选择
                final Comboitem cbItem = new Comboitem();
                cbItem.setLabel(label);
                cbItem.setValue(val);
                cbItem.setParent(cbox);
                cbox.setSelectedItem(cbItem);
            }
        }
    }
    
    /**
     * render.
     * @param cbox Combobox
     * @param objs List<Object>
     * @param labelPropName String
     * @param addfirstitem boolean
     * @param addfirstlabel String
     */
    public static void render(final Combobox cbox, final List objs, final String labelPropName,
        final boolean addfirstitem, final String addfirstlabel) {
        if (cbox != null && cbox.getItems() != null && cbox.getItems().size() > 0) {
            cbox.getItems().clear();
        }
        if (cbox == null || objs == null) {
            return; // throw new IllegalArgumentException("传入的组件或者是值编码不能为空!");
        }
        if (addfirstitem) {
            new Comboitem(addfirstlabel == null ? ""
                : addfirstlabel).setParent(cbox);
        }
        for (final Object obj : objs) {
            final String lab = "" + BeanUtilsExtend.getAttr(obj, labelPropName);
            final Comboitem item = new Comboitem(lab);
            item.setValue(obj);
            item.setParent(cbox);
        }
        if (cbox.getItems().size() != 0 && cbox.getSelectedItem() == null) {
            cbox.setSelectedIndex(0);
        }
    }
    
    /**
     * render.
     * 
     * @param cbox Combobox
     * @param slist List<String>
     * @author wuyx
     * 2011-7-6 wuyx 
     */
    public static void render(final Combobox cbox, final List<String> slist) {
        if (cbox != null && cbox.getItems() != null && cbox.getItems().size() > 0) {
            cbox.getItems().clear();
        }
        if (slist != null && slist.size() > 0) {
            for (final String str : slist) {
                final Comboitem item = new Comboitem(str);
                item.setValue(str);
                item.setParent(cbox);
            }
        }
    }
    
}
